﻿Public Class Empresas 
    Public Sub Mostrar()
        If guardar.Enabled = False Then
            guardar.Enabled = True
            cancelar.Enabled = True
            agregar.Enabled = False
            modificar.Enabled = False
            eliminar.Enabled = False
            imprimir.Enabled = False
            PanelArriba.Enabled = True
            PanelAbajo.Enabled = False
        Else
            cancelar.Enabled = False
            guardar.Enabled = False
            agregar.Enabled = True
            Modificar.Enabled = True
            Eliminar.Enabled = True
            imprimir.Enabled = True
            PanelArriba.Enabled = False
            PanelAbajo.Enabled = True

        End If
    End Sub
    Private Sub EmpresasBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmpresasBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.EmpresasBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DbFireAndIceDataSet)

    End Sub

    Private Sub Empresas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DbFireAndIceDataSet.empresas' table. You can move, or remove it, as needed.
        Me.EmpresasTableAdapter.Fill(Me.DbFireAndIceDataSet.empresas)

    End Sub

    Private Sub agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles agregar.Click
        BindingNavigatorAddNewItem.PerformClick()
        Mostrar()
    End Sub

    Private Sub modificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles modificar.Click
        Mostrar()
    End Sub

    Private Sub eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eliminar.Click
        Dim opcion As Integer
        opcion = MsgBox("¿Está seguro que desea eliminar el registro?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "Eliminar registro")
        If opcion = vbYes Then
            Try
                EmpresasTableAdapter.DeleteQuery(IdempresaTextBox.Text)
                Me.EmpresasTableAdapter.Fill(Me.DbFireAndIceDataSet.empresas)
            Catch ex As Exception
                MsgBox("Lo sentimos, el registro no puede ser eliminado ya que está relacionado con otras tablas", MsgBoxStyle.Critical, "Error")
            End Try


        End If
    End Sub

    Private Sub cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelar.Click
        ErrorProvider1.SetError(DescripcionTextBox, Nothing)
        ErrorProvider1.SetError(MontosubsidioTextBox, Nothing)
        ErrorProvider1.SetError(Telefono1TextBox, Nothing)
        Mostrar()
        Me.EmpresasTableAdapter.Fill(Me.DbFireAndIceDataSet.empresas)
    End Sub

    Private Sub guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles guardar.Click

        If (DescripcionTextBox.Text.Length = 0) Then
            ErrorProvider1.SetError(DescripcionTextBox, "Debe ingresar un nombre")


        End If

        If (MontosubsidioTextBox.Text.Length = 0) Then
            ErrorProvider1.SetError(MontosubsidioTextBox, "Debe ingresar un Apellido")

        End If

        If (Telefono1TextBox.Text.Length = 0) Then
            ErrorProvider1.SetError(Telefono1TextBox, "Debe seleccionar una empresa")
        End If

        If ((DescripcionTextBox.Text.Length = 0) And (MontosubsidioTextBox.Text.Length = 0) And (Telefono1TextBox.Text.Length = 0)) Then
            ErrorProvider1.SetError(DescripcionTextBox, "Debe ingresar un nombre")
            ErrorProvider1.SetError(MontosubsidioTextBox, "Debe ingresar un Apellido")
            ErrorProvider1.SetError(Telefono1TextBox, "Debe seleccionar una empresa")

        Else
            ErrorProvider1.SetError(DescripcionTextBox, Nothing)
            ErrorProvider1.SetError(MontosubsidioTextBox, Nothing)
            ErrorProvider1.SetError(Telefono1TextBox, Nothing)
            EmpresasBindingNavigatorSaveItem.PerformClick()
            MessageBox.Show("Se guardó el resgistro satisfactoriamente")
            Mostrar()

        End If
    End Sub

    Private Sub imprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imprimir.Click
        GridControl1.ShowPrintPreview()

    End Sub
End Class